/******************************************************************************************
 Paper Title:   The Correlates of Brazil's Military Expenditures, 1822–1945: 
                The Politics of Defense Policy in a Non-Democratic Era

 Authors:
    Octavio Amorim Neto
    Fundação Getulio Vargas (FGV), Escola Brasileira de Administração Pública e de Empresas (EBAPE)
    Rio de Janeiro, RJ, Brazil
    ORCID: https://orcid.org/0000-0002-3891-3361
    Contact: octavio.amorim.neto@fgv.br  [Corresponding author]

    Igor Acácio
    California State University, Fullerton
    Division of Politics, Administration and Justice
    Fullerton, California, United States
    ORCID: https://orcid.org/0000-0001-9998-0683

 Purpose:       Replication file for figures and tables in the BPSR manuscript

 Dataset:       Final Dataset BPSR.dta
******************************************************************************************/

*----------------------------------------------------------------------------------------*
* LOAD DATA
*----------------------------------------------------------------------------------------*
use "/Users/igoracacio/CSU Fullerton Dropbox/Igor Acácio/Final Dataset BPSR.dta", clear

* Note: Some figure/table elements were manually adjusted in the manuscript.


** Required user-written packages:
*   - asdoc    (export tables to Word/RTF)
*       ssc install asdoc, replace
*   - outreg2  (export regression results to tables)
*       ssc install outreg2, replace
*   - coefplot (plot regression coefficients)
*       ssc install coefplot, replace
*
* Make sure the packages above are installed before running


* Lag-order selection criteria
asdoc varsoc militaryexp, maxlag(10)

* Partial autocorrelation up to 10 lags (95% confidence bands)
pac militaryexp, lags(10) level(95)


* =========================================================
* Stationarity and Cointegration Diagnostics
* =========================================================

* --------
* Test for Stationarity with Augmented Dickey-Fuller (ADF)
* --------

dfuller militaryexp
dfuller L.militaryexp

dfuller discount
dfuller militaryministers
dfuller tensionscore
dfuller v2x_polyarchy
dfuller ratiohegemon
dfuller civconflict
dfuller intconflict
dfuller e_gdppc



* =========================================================
* Autocorrelatoon
* =========================================================

* (a) Model with relative capabilities
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy ratiohegemon civconflict intconflict e_gdppc

* Durbin–Watson test (AR1)
estat dwatson

* Breusch–Godfrey LM test (AR up to lag 1)
estat bgodfrey, lags(1)


* (b) Model with geopolitical competition
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy comp_geo civconflict intconflict e_gdppc

* Durbin–Watson test (AR1)
estat dwatson

* Breusch–Godfrey LM test (AR up to lag 1)
estat bgodfrey, lags(1)




* =========================================================
* Cointegration Tests (Engle–Granger two-step)
* =========================================================

* (a) Model with relative capabilities
* Step 1: Estimate long-run relationship (example with relative capabilities)
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy ratiohegemon civconflict intconflict e_gdppc

* Step 2: Save residuals
predict residuals, resid

* Step 3: ADF test on residuals
dfuller residuals, lag(1) drift

drop residuals


* (b) Model with geopolitical competition
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy comp_geo civconflict intconflict e_gdppc
estat bgodfrey, lags(1)

* ---- Step 2: Save residuals ----
predict residuals, resid

* ---- Step 3: ADF test on residuals ----
* Null: residuals are non-stationary (no cointegration).
* Rejecting null = evidence of cointegration.
dfuller residuals, lag(1) drift

drop residuals


* =========================================================
* Heteroskedasticity Diagnostics for Military Expenditure Models
* =========================================================

* (a) Model with relative capabilities
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy ratiohegemon civconflict intconflict e_gdppc

* Breusch–Pagan / Cook–Weisberg test
estat hettest

* White's test
estat imtest, white


* (b) Model with geopolitical competition
reg militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy comp_geo civconflict intconflict e_gdppc

* Breusch–Pagan / Cook–Weisberg test
estat hettest

* White's test
estat imtest, white



*----------------------------------------------------------------------------------------*
* FIGURE 1 – Defense expenditures as a share of cabinet portfolio expenditures (1822-1945)
*----------------------------------------------------------------------------------------*


* Note: Some annotations were adjusted manually in the figure.
tsset year
tsline militaryexp, ///
    tlabel(1822(8)1945, angle(0)) ///
    ylabel(0(20)80) ///
    ytitle("Percentage") ///
    xtitle("Year") ///
    tline(1889 1930 1945, lpattern(dash) lcolor(black)) ///
	    ylabel(, axis(1) labcolor(black) angle(horizontal)) ///
    xlabel(, labcolor(black)) ///
    text(80 1855 "Monarchical Period", size(small) place(c) bcolor(white) lcolor(white) box) ///
    text(80 1910 "First Republic", size(small) place(c) bcolor(white) lcolor(white) box) ///
    text(80 1938 "Vargas Era", size(small) place(c) bcolor(white) lcolor(white) box) ///
    legend(off)
	

graph export "/Users/igoracacio/CSU Fullerton Dropbox/Igor Acácio/Figure 1 – Defense expenditures as a share of cabinet portfolio expenditures (1822-1945).jpg", as(jpg) name("Graph") quality(100)


*----------------------------------------------------------------------------------------*
* TABLE 3 – Descriptive Statistics
*----------------------------------------------------------------------------------------*
asdoc sum militaryexp militaryexplag discount militaryministers tensionscore ///
    v2x_polyarchy ratiohegemon comp_geo civconflict intconflict e_gdppc, ///
    label replace ///
    title(Table 3 – Descriptive Statistics)

*----------------------------------------------------------------------------------------*
* TABLE 4 – The Correlates of Military Expenditures in Brazil (1822–1945)
*----------------------------------------------------------------------------------------*

* Estimate models using Newey-West standard errors (lag 1)
*----------------------------------*
* Model 1 – Baseline
newey militaryexp discount militaryministers tensionscore v2x_polyarchy ///
    ratiohegemon intconflict civconflict e_gdppc, lag(1)
est store m1

* Model 2 – Add lagged dependent variable
newey militaryexp discount militaryministers tensionscore v2x_polyarchy ///
    ratiohegemon intconflict civconflict e_gdppc militaryexplag, lag(1)
est store m2

* Model 3 – Replace relative capabilities with geopolitical competition
newey militaryexp discount militaryministers tensionscore v2x_polyarchy ///
    comp_geo intconflict civconflict e_gdppc, lag(1)
est store m3

* Model 4 – Model 3 + lagged DV
newey militaryexp discount militaryministers tensionscore v2x_polyarchy ///
    comp_geo intconflict civconflict e_gdppc militaryexplag, lag(1)
est store m4

* Export results
outreg2 [m1 m2 m3 m4] using military_expenditures_reg.doc, ///
    replace dec(3) label ///
    addnote("Newey-West standard errors; * p<0.10, ** p<0.05, *** p<0.01")

* Note: F-statistics and additional notes were added manually in Word.

*----------------------------------------------------------------------------------------*
* FIGURES 2 & 3 – Standardized Coefficients with 90% Confidence Intervals
*----------------------------------------------------------------------------------------*

* STEP 1 – Standardize all variables
foreach var in militaryexp discount militaryministers tensionscore v2x_polyarchy ///
              ratiohegemon comp_geo intconflict civconflict e_gdppc militaryexplag {
    egen z_`var' = std(`var')
}

* STEP 2 – Estimate standardized Models 1 and 2
newey z_militaryexp z_discount z_militaryministers z_tensionscore z_v2x_polyarchy ///
      z_ratiohegemon z_intconflict z_civconflict z_e_gdppc, lag(1)
est store m1_std

newey z_militaryexp z_discount z_militaryministers z_tensionscore z_v2x_polyarchy ///
      z_ratiohegemon z_intconflict z_civconflict z_e_gdppc z_militaryexplag, lag(1)
est store m2_std

* Variable labels for clarity in plots
label variable z_discount              "Discount Rate"
label variable z_militaryministers     "% of Military Officers in Cabinet"
label variable z_tensionscore          "Weighted Military Tension Score"
label variable z_v2x_polyarchy         "Electoral Democracy Index"
label variable z_ratiohegemon          "Relative Capabilities (BR vs. Hegemon)"
label variable z_intconflict           "International Conflict"
label variable z_civconflict           "Domestic Conflict"
label variable z_e_gdppc               "GDP Per Capita"
label variable z_comp_geo              "Geopolitical Competition"
label variable z_militaryexplag        "Military Expenditures (t-1)"

* FIGURE 2 – Coefficient Plot (Models 1 & 2)

*----------------------------------------------------------------------------------------*
* FIGURE 2 – Figure 2 - Standardized Coefficients, 90% Confidence Intervals   (Models 1 and 2
*----------------------------------------------------------------------------------------*

	coefplot ///
    (m1_std, msymbol(triangle) mcolor(navy) msize(medium) label("Model 1") ///
             ciopts(lpattern(shortdash_dot) lcolor(navy) lwidth(medthick))) ///
    (m2_std, msymbol(diamond) mcolor(black) msize(medium) label("Model 2, Lagged DV") ///
             ciopts(lpattern(solid) lcolor(black) lwidth(medthick))), ///
    drop(_cons z_militaryexplag) ///
    level(90) ///
    xline(0, lpattern(dash) lcolor(black)) ///
    legend(position(6) row(1) colfirst region(style(none))) ///
    yscale(reverse) ///
    ylabel(, axis(1) labcolor(black) angle(horizontal) nogrid) ///
    xlabel(, labcolor(black)) ///
    mlabposition(12) mlabcolor(black) mlabsize(small)


graph export "/Users/igoracacio/CSU Fullerton Dropbox/Igor Acácio/Figure 2 - Standardized Coefficients, 90% Confidence Intervals   (Models 1 and 2).jpg", as(jpg) name("Graph") quality(100) replace


* STEP 3 – Estimate standardized Models 3 and 4


Figure 3 - Standardized Coefficients, 90% Confidence Intervals  (Models 3 and 4)

newey z_militaryexp z_discount z_militaryministers z_tensionscore z_v2x_polyarchy ///
      z_comp_geo z_intconflict z_civconflict z_e_gdppc, lag(1)
est store m3_std

newey z_militaryexp z_discount z_militaryministers z_tensionscore z_v2x_polyarchy ///
      z_comp_geo z_intconflict z_civconflict z_e_gdppc z_militaryexplag, lag(1)
est store m4_std


*----------------------------------------------------------------------------------------*
* FIGURE 3 – Standardized Coefficients, 90% Confidence Intervals  (Models 3 and 4)
*----------------------------------------------------------------------------------------*

coefplot ///
    (m3_std, msymbol(triangle) mcolor(navy) msize(medium) label("Model 3") ///
             ciopts(lpattern(shortdash_dot) lcolor(navy) lwidth(medthick))) ///
    (m4_std, msymbol(diamond) mcolor(black) msize(medium) label("Model 4, Lagged DV") ///
             ciopts(lpattern(solid) lcolor(black) lwidth(medthick))), ///
    drop(_cons z_militaryexplag) ///
    level(90) ///
    xline(0, lpattern(dash) lcolor(black)) ///
    legend(position(6) row(1) colfirst region(style(none))) ///
    yscale(reverse) ///
    ylabel(, axis(1) labcolor(black) angle(horizontal) nogrid) ///
    xlabel(, labcolor(black)) ///
    mlabposition(12) mlabcolor(black) mlabsize(small)


	
graph export "/Users/igoracacio/CSU Fullerton Dropbox/Igor Acácio/Figure 3 - Standardized Coefficients, 90% Confidence Intervals  (Models 3 and 4).jpg", as(jpg) name("Graph") quality(100) replace

*----------------------------------------------------------------------------------------*
* FIGURE 4 – Correlation Map of Explanatory Variables
*----------------------------------------------------------------------------------------*
* Not produced in Stata – see companion file:
* → "BPSR_Bivariadas.ipynb"

*----------------------------------------------------------------------------------------*
* TABLE 5 – Bivariate Analysis of Correlates of Military Expenditures
*----------------------------------------------------------------------------------------*
* Not produced in Stata – see companion file:
* → "BPSR_Bivariadas.ipynb"































